﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;

namespace Shared.Method
{
    public class MacClass
    {
        /// <summary>
        /// 获取IP网段对应的MAC地址
        /// </summary>
        /// <param name="serverNetwork"></param>
        /// <returns></returns>
        public static string GetLocalMac(string serverNetwork)
        {
            try
            {
                string strMac = string.Empty;
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        var count = (from ip in (string[])mo["IPAddress"]
                                     where ip.Contains(serverNetwork)
                                     select ip
                                   ).Count();

                        if (count >= 1)
                        {
                            strMac = mo["MacAddress"].ToString();
                            break;
                        }
                    }
                }
                return strMac;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获取MAC地址
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static string GetModemMacAddress(YcdTelnetClass tel)
        {
            if (tel == null) return null;

            tel.Send("flash get ELAN_MAC_ADDR");

            string msg;
            bool hasKey = tel.GetReturnMsg("ELAN_MAC_ADDR=", out msg);

            if (hasKey == false || string.IsNullOrEmpty(msg))//获取MAC的第二种方法
            {
                tel.Send("ifconfig br0");

                hasKey = tel.GetReturnMsg("BROADCAST", out msg);

                if (hasKey == false)
                {
                    return null;
                }

                string[] msgArray = msg.Split(' ');
                //从msg中得到MAC地址
                string macMsg = (from s in msgArray
                                 where s.Contains(":") && s.Length == 17
                                 select s).FirstOrDefault();

                //获得MAC地址字符串
                if (macMsg != null) macMsg = macMsg.Replace(":", "");

                return macMsg;
            }

            string mac = msg.Substring(msg.IndexOf("=")+1,12);

            return mac.ToUpper();//转为大写的MAC
        }

        /// <summary>
        /// 获取MAC地址
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static string GetModemMacAddress(YcdTelnetSecond tel)
        {
            if (tel == null) return null;

            //tel.Send("flash get ELAN_MAC_ADDR");

            string msg;
            //bool hasKey = tel.GetReturnMsg("ELAN_MAC_ADDR=", out msg);

            bool hasKey=tel.SendAndRecv("flash get ELAN_MAC_ADDR", "ELAN_MAC_ADDR=", out msg);

            if (hasKey == false || string.IsNullOrEmpty(msg))//获取MAC的第二种方法
            {
                //tel.Send("ifconfig br0");

                //hasKey = tel.GetReturnMsg("BROADCAST", out msg);

                hasKey = tel.SendAndRecv("ifconfig br0", "BROADCAST", out msg);

                if (hasKey == false)
                {
                    return null;
                }

                string[] msgArray = msg.Split(' ');
                //从msg中得到MAC地址
                string macMsg = (from s in msgArray
                                 where s.Contains(":") && s.Length == 17
                                 select s).FirstOrDefault();

                //获得MAC地址字符串
                if (macMsg != null) macMsg = macMsg.Replace(":", "");

                return macMsg;
            }

            string mac = msg.Substring(msg.IndexOf("=") + 1, 12);

            return mac.ToUpper();//转为大写的MAC
        }
    }
}
